<template>
  <div class="app-container">
    <el-row :gutter="20">
			<el-col :span="5" >
				<div class="tltle" style="height:28px">
					水管机构
				</div> 
				<div class="grid-content bg-purple">
					<el-tree :data="deptOptions" default-expand-all node-key="orgCd" :default-expanded-keys="[]" :default-checked-keys="[]" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
				</div>
			</el-col>
			<el-col :span="19">
        <el-row style="height:28px" :gutter="10" class="mb8" type="flex" align="middle" justify="space-between">
          <el-col :span="1.5">
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item>水管机构</el-breadcrumb-item>
              <el-breadcrumb-item v-for="(item,index) in levelList" :key="index">
                <span v-if="index==levelList.length-1" class="no-redirect">{{item}}</span>
                <span v-else>{{ item }}</span>
              </el-breadcrumb-item>
            </el-breadcrumb>
          </el-col>
        </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计收年度" prop="feeYr">
        <el-date-picker
          v-model="queryParams.feeYr"
          type="year"
          format="yyyy"
          value-format="yyyy"
          placeholder="选择计收年度">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="缴费时间" prop="lastTime">
        <el-date-picker clearable
          v-model="queryParams.lastTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后缴费时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收费人" prop="lastOper">
        <el-input
          v-model="queryParams.lastOper"
          placeholder="请输入收费人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
  
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['waterPrice:WiOrgacctY:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['waterPrice:WiOrgacctY:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['waterPrice:WiOrgacctY:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['waterPrice:WiOrgacctY:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="WiOrgacctYList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="水费年度" align="center" prop="feeYr" />
      <!-- <el-table-column label="水管机构" align="center" prop="orgNm" /> -->
      <el-table-column label="农户" align="center" prop="farmNm" />
      <el-table-column label="应收水费" align="center" prop="totalFee">
      </el-table-column>
      <!-- <el-table-column label="缴费次数" align="center" prop="cashNum">
      </el-table-column> -->
      <el-table-column label="实收水费" align="center" prop="payFee">
      </el-table-column>
      <el-table-column label="尚欠水费" align="center" prop="saveFee">
      </el-table-column>
      <el-table-column label="最后缴费时间" align="center" prop="lastTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="收费人" align="center" prop="lastOper">
      </el-table-column>
      <el-table-column label="记录时间" align="center" prop="writeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.writeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="orgMark">
       
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['waterPrice:WiOrgacctY:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['waterPrice:WiOrgacctY:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-col>
    </el-row>
    <!-- 添加或修改机构水费台账登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" inline :model="form" :rules="rules" label-width="120px">
        <el-form-item label="应收水费" prop="totalFee">
          <el-input class="w202" v-model="form.totalFee" placeholder="请输入应收水费" />
        </el-form-item>
        <el-form-item label="缴费次数" prop="cashNum">
          <el-input class="w202" v-model="form.cashNum" :disabled="notEdit" placeholder="请输入缴费次数" />
        </el-form-item>
        <el-form-item label="实收水费" prop="payFee">
          <el-input class="w202" v-model="form.payFee" :disabled="notEdit" placeholder="请输入实收水费" />
        </el-form-item>
        <el-form-item label="尚欠水费" prop="saveFee">
          <el-input class="w202" v-model="form.saveFee" :disabled="notEdit" placeholder="请输入尚欠水费" />
        </el-form-item>
        <el-form-item label="最后缴费时间" prop="lastTime">
          <el-date-picker clearable
            v-model="form.lastTime"
            type="date"
            class="w202"
            :disabled="notEdit"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后缴费时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收费人" prop="lastOper">
          <el-input class="w202" :disabled="notEdit" v-model="form.lastOper" placeholder="请输入收费人" />
        </el-form-item>
        <el-form-item label="记录时间" prop="writeTime">
          <el-date-picker clearable
            v-model="form.writeTime"
            type="date"
            class="w202"
            :disabled="notEdit"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="orgMark">
          <el-input class="w202" v-model="form.orgMark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWiOrgacctY, getWiOrgacctY, delWiOrgacctY, addWiOrgacctY, updateWiOrgacctY } from "@/api/waterPrice/WiOrgacctY";
import { organtreeList } from "@/api/common";
export default {
  name: "WiOrgacctY",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机构水费台账登记表格数据
      WiOrgacctYList: [],
      deptOptions:[],
      levelList:[],
      defaultProps: {
        children: 'children',
        label: 'orgNm'
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //不能编辑
      notEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        totalFee: null,
        cashNum: null,
        payFee: null,
        saveFee: null,
        lastTime: null,
        lastOper: null,
        writeTime: null,
        orgMark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect()
  },
  methods: {
    getOrgIdArr(data, val) { //递归父级key
      for (const element of data) {
        if (element.orgNm == val) {
          return [element.orgNm];
        }
        if (element.children) {
          var far = this.getOrgIdArr(element.children, val);
          if (far) {
            return far.concat(element.orgNm);
          }
        }
      }
    },
     handleNodeClick(data) {
      if (data) {
        const result = this.getOrgIdArr(this.deptOptions, data.orgNm).reverse()
        this.levelList = result
        this.queryParams.orgCd = data.orgCd
        this.getList();
      }
    },
    /** 查询行政区域下拉树结构 */
    getTreeselect() {
      organtreeList({}).then((response) => {
        // response.data.forEach(element => {
        //   if(element.orgLevel == 2 || element.orgLevel == 3  ){
        //     element.isDisabled = false
        //   }
        // });
        this.deptOptions = this.handleTree(response.data, "orgCd", "parentOrgCd");
        this.handleNodeClick(response.data[0])
      });
    },
    /** 查询机构水费台账登记列表 */
    getList() {
      this.loading = true;
      listWiOrgacctY(this.queryParams).then(response => {
        this.WiOrgacctYList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        feeYr: null,
        orgCd: null,
        totalFee: null,
        cashNum: null,
        payFee: null,
        saveFee: null,
        lastTime: null,
        lastOper: null,
        writeTime: null,
        orgMark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.feeYr)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.notEdit = false;
      this.title = "添加机构水费台账登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.notEdit = true
      const feeYr = row.feeYr || this.ids
      // getWiOrgacctY(feeYr).then(response => {
        this.form = row;
        this.open = true;
        this.title = "修改机构水费台账登记";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.feeYr != null) {
            updateWiOrgacctY(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWiOrgacctY(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const feeYrs = row.feeYr || this.ids;
      this.$modal.confirm('是否确认删除机构水费台账登记编号为"' + feeYrs + '"的数据项？').then(function() {
        return delWiOrgacctY(feeYrs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('waterPrice/WiOrgacctY/export', {
        ...this.queryParams
      }, `水费台账_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.grid-content{
  height: 700px;
  overflow-y: scroll;
}
</style>
